import {Model} from './model';
import {SessionHistory} from './session';
import {internal} from './api';

export function GUI(name, content) {

    var gui = {};

    gui.model = new Model(gui);
    gui.session = new SessionHistory(gui);

    var model = gui.model;
    var importCount = 0;
    // var useQuickView = opts.quick_view; // may be set by mapshaper-gui
    var queuedFiles = [];
    // var manifestFiles = opts.files || [];
    var cachedFiles = {};
    var catalog;

    var importOpts = {"no_repair":false,"snap":false};

    importNewDataset("json", name, content, importOpts);

    return gui;

    function addDataset(dataset) {
        model.addDataset(dataset);
        importCount++;
    }

    function importNewDataset(fileType, fileName, content, importOpts) {
        var dataset;
        var input = {};
        try {
            input[fileType] = {filename: fileName, content: content};

            dataset = internal.importContent(input, importOpts);
            // save import options for use by repair control, etc.
            dataset.info.import_options = importOpts;
            gui.session.fileImported(fileName, readImportOptsAsString());
            addDataset(dataset);

        } catch(e) {
            console.trace(e);
            // handleImportError(e, fileName);
        }
    }

    function readImportOptsAsString() {
        return "";
    }

}
